package wnhz.com.business.groupshop.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import wnhz.com.business.R;
import wnhz.com.business.groupshop.adapter.MainGoodsAdapter;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.groupshop.bean.GoodsBean;
import wnhz.com.business.constants.EXTRA;
import wnhz.com.business.groupshop.bean.HomeIndexBean;
import wnhz.com.business.uitls.decoration.DividerGridItemDecoration;

/**
 * Created by Administrator on 2017/9/13 0013.
 */

public class ZongheFragment extends BaseFragment {

    @BindView(R.id.recycler_data)
    RecyclerView recyclerData;
    Unbinder unbinder;
    private View view;

    private List<HomeIndexBean.InfoBean.GoodsBean> goodsBeenList = new ArrayList<>();
    private MainGoodsAdapter mainGoodsAdapter;


    public static ZongheFragment newInstance() {
        return new ZongheFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_zonghe, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        setRecyclerData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initview()
    {

        recyclerData.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
    }

    private void setRecyclerData()
    {
        mainGoodsAdapter = new MainGoodsAdapter(getContext(), goodsBeenList);
        mainGoodsAdapter.loadDataFirstTime();

        GridLayoutManager gridManager = new GridLayoutManager(getContext(), 2,
                LinearLayoutManager.VERTICAL, false);  //自定义解决RecyclerView在ScrollView中滑动卡顿现象
//        gridManager.setScrollEnabled(false);

        recyclerData.setLayoutManager(gridManager);
        recyclerData.addItemDecoration(new DividerGridItemDecoration(20));//分割线
        recyclerData.setAdapter(mainGoodsAdapter);

        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int spansize = 1;
                switch (mainGoodsAdapter.getItemViewType(position)) {
                    case EXTRA.TYPE_FOOTER:
                        spansize = 2;
                        break;
                    case EXTRA.TYPE_HEADER:
                        spansize = 2;
                        break;
                }
                return spansize;
            }
        });


        mainGoodsAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
                GoodsBean goodsBean = (GoodsBean)data;


            }
        });
    }
}
